package com.gopay.common.cps.manager.order.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;




import com.gopay.common.cps.dao.order.CpsGeneralAssistOrderQueryDAO;
import com.gopay.common.cps.dao.order.CpsGeneralMainOrderQueryDAO;
import com.gopay.common.cps.manager.order.FullCpsGenAssistOrderQueryManager;
import com.gopay.common.domain.cps.CpsGenAssistOrder;
import com.gopay.common.domain.cps.CpsGenMainOrder;
import com.gopay.common.domain.cps.vo.FullCpsGenAssistOrder;

/**
 * 
 * @FullCpsGenAssistOrderQueryManagerImpl.java
 * @author liujy
 * @2013-1-21 上午11:51:46 www.gopay.com.cn Inc.All rights reserved.
 */
@Service("fullCpsGenAssistOrderQueryManager")
public class FullCpsGenAssistOrderQueryManagerImpl implements FullCpsGenAssistOrderQueryManager {
    /** 带参数查询订单：担保支付取消通过原订单交易时间、原商户订单号查询担保支付申请订单 */
    private final String FIND_ORG_FULL_RECV_ORDER_HQL = "FROM CpsGenMainOrder m WHERE m.merOrderId =:merOrderId AND m.merTxnTm = to_timestamp(:merTxnTm, 'yyyyMMddhh24miss') ";
    
    @Resource(name = "cpsGeneralMainOrderQueryDAO")
    private CpsGeneralMainOrderQueryDAO cpsGeneralMainOrderQueryDAO;

    @Resource(name = "cpsGeneralAssistOrderQueryDAO")
    private CpsGeneralAssistOrderQueryDAO cpsGeneralAssistOrderQueryDAO;

    @Override
    public FullCpsGenAssistOrder get(Serializable id) {
        CpsGenMainOrder main = cpsGeneralMainOrderQueryDAO.find(CpsGenMainOrder.class, id);
        if (main != null) {
            CpsGenAssistOrder assist = cpsGeneralAssistOrderQueryDAO.find(CpsGenAssistOrder.class, id);
            if (assist != null) {
                return new FullCpsGenAssistOrder(main, assist);
            }
        }
        return null;
    }

    @Override
    public List<FullCpsGenAssistOrder> get(Map<Object, Object> paraMap) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List<FullCpsGenAssistOrder> getAll() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public FullCpsGenAssistOrder findFullCpsGenAssistOrder(Map<String, Object> para) {
        List<CpsGenMainOrder> mainList = cpsGeneralMainOrderQueryDAO.findByHql(this.FIND_ORG_FULL_RECV_ORDER_HQL, para);
        if (mainList != null && mainList.size() > 0) {
            CpsGenMainOrder main = mainList.get(0);
            CpsGenAssistOrder sub = cpsGeneralAssistOrderQueryDAO.find(CpsGenAssistOrder.class, main.getGopayOrderId());

            if (sub != null) {
                return new FullCpsGenAssistOrder(main, sub);
            }
        }
        return null;
    }

    @Override
    public List<CpsGenAssistOrder> findCpsGenAssistOrder(String otherRecvInfo,String lastUpdTrancode){
        String hql = "FROM CpsGenAssistOrder m WHERE m.otherRecvInfo=:otherRecvInfo AND m.lastUpdTrancode=:lastUpdTrancode";
        Map<String,Object> param = new HashMap<>();
        param.put("otherRecvInfo",otherRecvInfo);
        param.put("lastUpdTrancode",lastUpdTrancode);
        List<CpsGenAssistOrder> cpsGenAssistOrderList = cpsGeneralAssistOrderQueryDAO.findByHql(hql,param);
        return cpsGenAssistOrderList;
    }

}
